package com.raja.servingStation.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.raja.servingStation.common.constant.Constants;
import com.raja.servingStation.common.constant.MysqlPageVo;
import com.raja.servingStation.dao.EngineerDao;
import com.raja.servingStation.entity.Engineer;
import com.raja.servingStation.entity.IdAndAvg;
import com.raja.servingStation.entity.OrderByEngineer;
import com.raja.servingStation.service.EngineerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by zhangzhen on 2017/10/10.
 */
@Service
public class EngineerServiceImpl implements EngineerService {
    @Autowired
    private EngineerDao engineerDao;

    @Override
    public MysqlPageVo queryOrderListByPage(JSONObject body) throws Exception {
        Integer pageSize = body.getInteger("pageSize");
        Integer currPage = body.getInteger("currPage");
        MysqlPageVo<Engineer> mysqlPageVo = new MysqlPageVo<Engineer>();
        if (pageSize == null){
            pageSize = Constants.pageSize;
        }
        if (currPage == null){
            currPage = Constants.currPage;
        }
        mysqlPageVo.setPageSize(pageSize, currPage);
        int count = engineerDao.getEngineerCount().intValue();
        mysqlPageVo.setTotal(count);
        if(count >0) {
            List<Engineer> engineers = engineerDao.getEngineerList(mysqlPageVo);
            List<IdAndAvg> list = engineerDao.getAvgScore();
            for(Engineer e : engineers){
                for(int i = 0;i<list.size();i++){
                    if(list.get(i).getId().equals(e.getId())){
                        e.setScore(list.get(i).getAvg());
                    }
                }
            }
            mysqlPageVo.setData(engineers);
        }
        return mysqlPageVo;
    }

    @Override
    public MysqlPageVo getOrdersByEngineerId(JSONObject body,Integer stationId) throws Exception {
        String engineerId = body.getString("engineerId");
        Integer pageSize = body.getInteger("pageSize");
        Integer currPage = body.getInteger("currPage");
        MysqlPageVo<Engineer> mysqlPageVo = new MysqlPageVo<Engineer>();
        if (pageSize == null){
            pageSize = Constants.pageSize;
        }
        if (currPage == null){
            currPage = Constants.currPage;
        }
        mysqlPageVo.setPageSize(pageSize, currPage);
        int count = engineerDao.getCountOrdersByEngineerId(engineerId,stationId).intValue();
        mysqlPageVo.setTotal(count);
        if(count >0) {
            List<OrderByEngineer> orders = engineerDao.getOrdersByEngineerId(engineerId,stationId);
            for (OrderByEngineer o: orders) {
                BigDecimal moeny = engineerDao.getMoneyByOrderId(o.getOrderId());
//                if(moeny==null){
//                    o.setMoney(BigDecimal.valueOf(0));
//                }
                o.setMoney(moeny);
            }
            mysqlPageVo.setData(orders);
        }
        return mysqlPageVo;
    }

    @Override
    public Object addEngineer(JSONObject body) throws Exception {
        return engineerDao.addEngineer(body);
    }
}
